<template>
 <el-card class="box-card">
  <bread data1='权限管理' data2='角色列表'></bread>
  <el-row class="role_add">
    <el-col>
      <el-button type="success" @click="addRole">添加角色</el-button>
    </el-col>
    <el-table :data="rolelist" style="width: 100%" >
        <el-table-column type="expand" width="50">
         <!-- 角色点击出现新的表格  -->
         <!-- 主要思路是将一级作为一列 -->
          <template v-slot='scope'>
            <el-row v-for='item in scope.row.children' :key='item.id' >
               <el-col :span='4'>
                 <!-- 存放一级权限 -->
                 <el-tag
                   closable
                   @close = 'delRight(scope.row,item.id)'

                    >{{item.authName}}</el-tag>
                 <!-- 添加右箭头样式 -->
                 <i class="el-icon-arrow-right"></i>
               </el-col>
               <el-col :span='20'>
                 <el-row v-for='itemson in item.children' :key='itemson.id'>
                   <!-- 二级权限 -->
                   <el-col :span='4'>
                 <el-tag
                   type="success"
                   closable
                   @close = 'delRight(scope.row,itemson.id)'
                   >{{itemson.authName}}</el-tag>
                 <!-- 添加右箭头样式 -->
                 <i class="el-icon-arrow-right"></i>
                   </el-col>
                   <el-col :span='20'>
                      <!-- 三级权限,所有权限需要在一列中展示 tag是一个单元格 -->
                     <el-tag v-for='item2 in itemson.children' :key='item2.id'
                      closable
                      type='warning'
                      @close = 'delRight(scope.row,item2.id)'
                     >{{item2.authName}}</el-tag>
                   </el-col>
                 </el-row>
               </el-col>
            </el-row>
            <!-- 无权限的时候 -->
            <span v-if='scope.row.children.length === 0'>未分配权限</span>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称" >
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width=""> </el-table-column>
        <el-table-column prop="address" label="操作" width="">
          <template v-slot="scope">
            <el-row>
                   <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                size="mini"
                plain
                @click="edtiorRight(scope.row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                size="mini"
                plain
                @click="delRole(scope.row.id)"
              ></el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
  </el-row>
    <!--添加角色对话框 -->
    <el-dialog title="添加角色" :visible.sync="dialogFormVisibleAdd">
      <el-form :model="form">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" >
          <el-input
            v-model="form.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加权限对话框 -->
        <el-dialog title="修改权限" :visible.sync="dialogFormVisibleRight">
        <!-- default-expand-all可以默认展开所有
         :default-expanded-keys="[所有权限id]" 是有for的嵌套 可以把所有的id放到一个数组中去
         :default-checked-keys有哪些权限被默认选中
          -->
          <el-tree
            ref='tree'
            :data="data"
            show-checkbox
            node-key="id"
            default-expand-all
            :default-checked-keys="arrCheck"
            :props="defaultProps">
          </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRight = false">取 消</el-button>
        <el-button type="primary" @click="addRight">确 定</el-button>
      </div>
    </el-dialog>
</el-card>
</template>

<script>

export default {
  data () {
    return {
      rolelist: [],
      dialogFormVisibleAdd: false,
      dialogFormVisibleRight: false,
      form: {
        roleName: '',
        roleDesc: ''
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      // arrexpand:[],
      arrCheck: [],
      // 当前修改权限的角色ID
      currentRoleId: -1
    }
  },
  created () {
    this.getRole()
  },
  methods: {
    // 删除角色
    async delRole (id) {
      const res = await this.$http.delete(`roles/${id}`)
      const { meta: { msg, status } } = res.data
      if (status === 200) {
        this.$message.success(msg)
        this.getRole()
      } else {
        this.$message.error(msg)
      }
    },
    // 添加权限
    async edtiorRight (right) {
      this.arrCheck = []
      this.dialogFormVisibleRight = true
      const res = await this.$http.get('rights/tree')
      this.data = res.data.data
      this.currentRoleId = right.id
      right.children.forEach(item => {
        this.arrCheck.push(item.id)
        item.children.forEach(item2 => {
          this.arrCheck.push(item2.id)
          item2.children.push(item3 => {
            this.arrCheck.push(item3.id)
          })
        })
      })
      //  把所有权限的id都放到一个数组中，来完成有默认打开的效果
    },
    async addRight () {
      // 当前被选中的key组成的数组
      // let arr1 =  this.$refs.tree.getCheckedKeys()
      // 当前被半选中的key组成的数组(子被选中)
      // let arr2 =  this.$refs.tree.getHalfCheckedKeys()
      // let arr = arr1.concat(arr2)
      // let arr  = [...arr1,...arr2]
      // let rids = arr.join(',')
      const rids = ([...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]).join(',')
      const res = await this.$http.post(`roles/${this.currentRoleId}/rights`, { rids })
      const { meta: { msg, status } } = res.data
      if (status === 200) {
        this.dialogFormVisibleRight = false
        this.$message.success(msg)
        this.getRole()
      } else {
        this.$message.error(msg)
      }
    },
    // 删除权限
    async delRight (role, rightId) {
      const res = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
      const { meta: { msg, status } } = res.data
      if (status === 200) {
        this.$message.success(msg)
        role.children = res.data.data
      } else {
        this.$message.error(msg)
      }
    },
    // 请求角色
    async getRole () {
      const res = await this.$http.get('roles')
      this.rolelist = res.data.data
    },
    //  添加角色
    addRole () {
      this.dialogFormVisibleAdd = true
    },
    async add () {
      const res = await this.$http.post('roles', this.form)
      const { meta: { msg, status } } = res.data
      if (status === 201) {
        this.dialogFormVisibleAdd = false
        this.$message.success(msg)
        for (const i in this.form) {
          this.form[i] = ''
        }
        this.getRole()
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.role_add{
  margin-top: 20px;
}
.el-col-24{
  margin-bottom:5px
}
.el-tag{
  cursor: pointer;
  margin-top: 5px;
  margin-right: 3px;
}
</style>
